/************************************
 * Package: RGcpp					*
 * Authors: Rui Eduardo Gouveia Gil	*
 * Date: 2010						*
 ************************************/
#ifndef __RGCPP_TCPSERVERSOCKET_H__
#define __RGCPP_TCPSERVERSOCKET_H__

#include <string>
#include <errno.h>
#include <string.h>
#ifdef _WIN32
#include <winsock2.h>
#else
#include <netdb.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#endif
#include <boost/thread/mutex.hpp>

#include "rgcpp/net/IPAddress.h"
#include "rgcpp/net/sockets/TCPSocket.h"

namespace rgcpp
{

#define DEFAULT_TCP_SERVER_SOCKET_DEPTH 5

class TCPServerSocket
{
	protected:
		int socketAccept;
		boost::mutex mut;
		bool releaseForced;
		unsigned int depth;
		IPAddress ipAddress;
		struct timeval timeout;

	public:
		TCPServerSocket();
		TCPServerSocket(const unsigned short port);
		TCPServerSocket(const IPAddress& ipAddress);
		~TCPServerSocket();

		TCPSocket* accept();

		void setTimeout(const unsigned long timeout);
		unsigned long getTimeout();

		void release();

		bool check4Accept(const unsigned long timeout=0);

		IPAddress getLocalAddress();

		void setDepth(const unsigned short value);
		unsigned int getDepth();
};

}

#endif
